package com.imooc.controller;

import com.imooc.common.constants.ResultCodeConstants;
import com.imooc.common.result.ResponseData;
import com.imooc.common.result.ResponseUtils;
import com.imooc.common.utils.CookieUtils;
import com.imooc.common.utils.JsonUtils;
import com.imooc.common.utils.MD5Utils;
import com.imooc.dto.user.UserLoginRequest;
import com.imooc.dto.user.UserLoginResponse;
import com.imooc.dto.user.UserRegistRequest;
import com.imooc.dto.user.UserRegistResponse;
import com.imooc.pojo.Users;
import com.imooc.pojo.bo.UserBO;
import com.imooc.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @ClassName: PassportController
 * @description: 注册登录API
 * @author: limx
 * @create: 2019-12-20 00:34
 **/
@RestController
@RequestMapping("/passport")
@Api(value = "注册登录", tags = {"用于注册登录的相关接口"})
public class PassportController {

    @Autowired
    private IUserService userService;

    @GetMapping("/usernameIsExist")
    @ApiOperation(value = "用户名是否存在", notes = "用户名是否存在", httpMethod = "GET")
    public ResponseData usernameIsExist(@RequestParam String  username){
        ResponseData response = userService.queryUsernameIsExist(username);
       return response;
    }

    @PostMapping("/regist")
    @ApiOperation(value = "用户注册", notes = "用户注册", httpMethod = "POST")
    public ResponseData regist(@RequestBody UserRegistRequest registRequest, HttpServletRequest request, HttpServletResponse response) throws Exception{
        UserRegistResponse registResponse = userService.createUser(registRequest);

        CookieUtils.setCookie(request, response, "user",
                JsonUtils.objectToJson(registResponse), true);

        return ResponseUtils.success(null);
    }

    @PostMapping("/login")
    @ApiOperation(value = "用户登录", notes = "用户登录", httpMethod = "POST")
    public ResponseData login(@RequestBody UserLoginRequest loginRequest,
              HttpServletRequest request, HttpServletResponse response) throws Exception {

        UserLoginResponse loginResponse = userService.userLogin(loginRequest);
        if(!loginResponse.getCode().equals(ResultCodeConstants.SUCCESS.getCode())){
            return ResponseUtils.errorMsg(loginResponse.getMsg());
        }
        CookieUtils.setCookie(request, response, "user",
                JsonUtils.objectToJson(loginResponse), true);
        return ResponseUtils.success(loginResponse);
    }

    @ApiOperation(value = "用户退出登录", notes = "用户退出登录", httpMethod = "POST")
    @PostMapping("/logout")
    public ResponseData logout(HttpServletRequest request,
                                  HttpServletResponse response) {
        // 清除用户的相关信息的cookie
        CookieUtils.deleteCookie(request, response, "user");
        return ResponseUtils.success();
    }


}
